 package handling.channel.handler;

 import client.MapleCharacter;
 import client.MapleClient;
 import client.inventory.Item;
 import client.inventory.ItemFlag;
 import client.inventory.ItemLoader;
 import client.inventory.MapleInventoryType;
 import com.alibaba.druid.pool.DruidPooledConnection;
 import constants.GameConstants;
 import constants.ItemConstants;
 import database.DatabaseConnection;
 import handling.channel.ChannelServer;
 import handling.world.WorldFindService;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import server.MapleDueyActions;
 import server.MapleInventoryManipulator;
 import server.MapleItemInformationProvider;
 import tools.MaplePacketCreator;
 import tools.Pair;
 import tools.data.input.LittleEndianAccessor;





 public class DueyHandler
 {
   public static void DueyOperation(LittleEndianAccessor slea, MapleClient c) {
     int conv;
     byte inventId;
     int packageid;
     short itemPos;
     MapleDueyActions dp;
     short amount;
     int mesos;
     String recipient;
     boolean quickdelivery;
     long finalcost;
/*  47 */     byte operation = slea.readByte();
/*  48 */     switch (operation) {

       case 1:
/*  51 */         conv = c.getPlayer().getConversation();
/*  52 */         if (conv == 2) {
/*  53 */           c.announce(MaplePacketCreator.sendDuey((byte)10, loadItems(c.getPlayer())));
         }
         return;

       case 3:
/*  58 */         if (c.getPlayer().getConversation() != 2) {
           return;
         }
/*  61 */         inventId = slea.readByte();
/*  62 */         itemPos = slea.readShort();
/*  63 */         amount = slea.readShort();
/*  64 */         mesos = slea.readInt();
/*  65 */         recipient = slea.readMapleAsciiString();
/*  66 */         quickdelivery = (slea.readByte() > 0);

/*  68 */         finalcost = mesos + GameConstants.getTaxAmount(mesos) + (quickdelivery ? 0L : 5000L);

/*  70 */         if (mesos >= 0 && mesos <= 100000000 && c.getPlayer().getMeso() >= finalcost) {
/*  71 */           int accid = getAccIdFromName(recipient, true);
/*  72 */           if (accid != -1) {
/*  73 */             if (accid != c.getAccID()) {
/*  74 */               boolean recipientOn = false;
/*  75 */               int chz = WorldFindService.getInstance().findChannel(recipient);
/*  76 */               if (chz > 0) {
/*  77 */                 MapleCharacter receiver = ChannelServer.getInstance(chz).getPlayerStorage().getCharacterByName(recipient);
/*  78 */                 if (receiver != null) {
/*  79 */                   recipientOn = true;
                 }
               }
/*  82 */               if (inventId > 0) {
/*  83 */                 MapleInventoryType inv = MapleInventoryType.getByType(inventId);
/*  84 */                 Item item = c.getPlayer().getInventory(inv).getItem(itemPos);
/*  85 */                 if (item == null) {
/*  86 */                   c.announce(MaplePacketCreator.sendDuey((byte)17, null));
                   return;
                 }
/*  89 */                 short flag = item.getFlag();
/*  90 */                 if (ItemFlag.不可交易.check(flag) || ItemFlag.封印.check(flag)) {
/*  91 */                   c.announce(MaplePacketCreator.enableActions());
                   return;
                 }
/*  94 */                 if (c.getPlayer().getItemQuantity(item.getItemId(), false) >= amount) {
/*  95 */                   MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
/*  96 */                   if (!ii.isDropRestricted(item.getItemId()) && !ii.isAccountShared(item.getItemId())) {
/*  97 */                     if (addItemToDB(item, amount, mesos, c.getPlayer().getName(), getAccIdFromName(recipient, false), recipientOn)) {
/*  98 */                       if (ItemConstants.is飞镖道具(item.getItemId()) || ItemConstants.is子弹道具(item.getItemId())) {
/*  99 */                         MapleInventoryManipulator.removeFromSlot(c, inv, (short)(byte)itemPos, item.getQuantity(), true);
                       } else {
/* 101 */                         MapleInventoryManipulator.removeFromSlot(c, inv, (short)(byte)itemPos, amount, true, false);
                       }
/* 103 */                       c.getPlayer().gainMeso(-finalcost, false);
/* 104 */                       c.announce(MaplePacketCreator.sendDuey((byte)19, null));
                     } else {
/* 106 */                       c.announce(MaplePacketCreator.sendDuey((byte)17, null));
                     }
                   } else {
/* 109 */                     c.announce(MaplePacketCreator.sendDuey((byte)17, null));
                   }
                 } else {
/* 112 */                   c.announce(MaplePacketCreator.sendDuey((byte)17, null));
                 }

/* 115 */               } else if (addMesoToDB(mesos, c.getPlayer().getName(), getAccIdFromName(recipient, false), recipientOn)) {
/* 116 */                 c.getPlayer().gainMeso(-finalcost, false);
/* 117 */                 c.announce(MaplePacketCreator.sendDuey((byte)19, null));
               } else {
/* 119 */                 c.announce(MaplePacketCreator.sendDuey((byte)17, null));
               }
             } else {

/* 123 */               c.announce(MaplePacketCreator.sendDuey((byte)15, null));
             }
           } else {
/* 126 */             c.announce(MaplePacketCreator.sendDuey((byte)14, null));
           }
         } else {
/* 129 */           c.announce(MaplePacketCreator.sendDuey((byte)12, null));
         }
         return;

       case 5:
/* 134 */         if (c.getPlayer().getConversation() != 2) {
           return;
         }
/* 137 */         packageid = slea.readInt();

/* 139 */         dp = loadSingleItem(packageid, c.getPlayer().getId());
/* 140 */         if (dp == null) {
           return;
         }
/* 143 */         if (dp.getItem() != null && !MapleInventoryManipulator.checkSpace(c, dp.getItem().getItemId(), dp.getItem().getQuantity(), dp.getItem().getOwner())) {
/* 144 */           c.announce(MaplePacketCreator.sendDuey((byte)22, null)); return;
         }
/* 146 */         if (dp.getMesos() < 0 || dp.getMesos() + c.getPlayer().getMeso() < 0L) {
/* 147 */           c.announce(MaplePacketCreator.sendDuey((byte)17, null));
           return;
         }
/* 150 */         removeItemFromDB(packageid, c.getPlayer().getId());
/* 151 */         if (dp.getItem() != null) {
/* 152 */           MapleInventoryManipulator.addFromDrop(c, dp.getItem(), false);
         }
/* 154 */         if (dp.getMesos() != 0) {
/* 155 */           c.getPlayer().gainMeso(dp.getMesos(), false);
         }
/* 157 */         c.announce(MaplePacketCreator.removeItemFromDuey(false, packageid));
         return;

       case 6:
/* 161 */         if (c.getPlayer().getConversation() != 2) {
           return;
         }
/* 164 */         packageid = slea.readInt();
/* 165 */         removeItemFromDB(packageid, c.getPlayer().getId());
/* 166 */         c.announce(MaplePacketCreator.removeItemFromDuey(true, packageid));
         return;

       case 8:
/* 170 */         c.getPlayer().setConversation(0);
         return;
     }

/* 174 */     System.out.println("Unhandled Duey operation : " + slea.toString());
   }




   private static boolean addMesoToDB(int mesos, String sName, int recipientID, boolean isOn) {
/* 181 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 182 */       PreparedStatement ps = con.prepareStatement("INSERT INTO dueypackages (RecieverId, SenderName, Mesos, TimeStamp, Checked, Type) VALUES (?, ?, ?, ?, ?, ?)");
/* 183 */       ps.setInt(1, recipientID);
/* 184 */       ps.setString(2, sName);
/* 185 */       ps.setInt(3, mesos);
/* 186 */       ps.setLong(4, System.currentTimeMillis());
/* 187 */       ps.setInt(5, isOn ? 0 : 1);
/* 188 */       ps.setInt(6, 3);

/* 190 */       ps.executeUpdate();
/* 191 */       ps.close();

/* 193 */       return true;
/* 194 */     } catch (SQLException se) {
/* 195 */       se.printStackTrace();
/* 196 */       return false;
     }
   }

   private static boolean addItemToDB(Item item, int quantity, int mesos, String sName, int recipientID, boolean isOn) {
/* 201 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 202 */       PreparedStatement ps = con.prepareStatement("INSERT INTO dueypackages (RecieverId, SenderName, Mesos, TimeStamp, Checked, Type) VALUES (?, ?, ?, ?, ?, ?)", 1);
/* 203 */       ps.setInt(1, recipientID);
/* 204 */       ps.setString(2, sName);
/* 205 */       ps.setInt(3, mesos);
/* 206 */       ps.setLong(4, System.currentTimeMillis());
/* 207 */       ps.setInt(5, isOn ? 0 : 1);

/* 209 */       ps.setInt(6, item.getType());
/* 210 */       ps.executeUpdate();
/* 211 */       ps.close();
/* 212 */       if (item != null) {
/* 213 */         ItemLoader.送货道具.saveItems(con, Collections.singletonList(new Pair(item, ItemConstants.getInventoryType(item.getItemId()))), recipientID);
       }
/* 215 */       return true;
/* 216 */     } catch (SQLException se) {
/* 217 */       se.printStackTrace();
/* 218 */       return false;
     }
   }

   public static List<MapleDueyActions> loadItems(MapleCharacter chr) {
/* 223 */     List<MapleDueyActions> packages = new LinkedList<>();
/* 224 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 225 */       PreparedStatement ps = con.prepareStatement("SELECT * FROM dueypackages WHERE RecieverId = ?");
/* 226 */       ps.setInt(1, chr.getId());
/* 227 */       ResultSet rs = ps.executeQuery();
/* 228 */       while (rs.next()) {
/* 229 */         MapleDueyActions dueypack = getItemByPID(rs.getInt("packageid"));
/* 230 */         dueypack.setSender(rs.getString("SenderName"));
/* 231 */         dueypack.setMesos(rs.getInt("Mesos"));
/* 232 */         dueypack.setSentTime(rs.getLong("TimeStamp"));
/* 233 */         packages.add(dueypack);
       }
/* 235 */       rs.close();
/* 236 */       ps.close();
/* 237 */       return packages;
/* 238 */     } catch (SQLException se) {
/* 239 */       se.printStackTrace();
/* 240 */       return null;
     }
   }

   public static MapleDueyActions loadSingleItem(int packageid, int charid) {
/* 245 */     List<MapleDueyActions> packages = new LinkedList<>();
/* 246 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 247 */       PreparedStatement ps = con.prepareStatement("SELECT * FROM dueypackages WHERE PackageId = ? AND RecieverId = ?");
/* 248 */       ps.setInt(1, packageid);
/* 249 */       ps.setInt(2, charid);
/* 250 */       ResultSet rs = ps.executeQuery();

/* 252 */       if (rs.next()) {
/* 253 */         MapleDueyActions dueypack = getItemByPID(packageid);
/* 254 */         dueypack.setSender(rs.getString("SenderName"));
/* 255 */         dueypack.setMesos(rs.getInt("Mesos"));
/* 256 */         dueypack.setSentTime(rs.getLong("TimeStamp"));
/* 257 */         packages.add(dueypack);
/* 258 */         rs.close();
/* 259 */         ps.close();
/* 260 */         return dueypack;
       }
/* 262 */       rs.close();
/* 263 */       ps.close();
/* 264 */       return null;
     }
/* 266 */     catch (SQLException se) {
/* 267 */       return null;
     }
   }

   public static void reciveMsg(MapleClient c, int recipientId) {
/* 272 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 273 */       PreparedStatement ps = con.prepareStatement("UPDATE dueypackages SET Checked = 0 WHERE RecieverId = ?");
/* 274 */       ps.setInt(1, recipientId);
/* 275 */       ps.executeUpdate();
/* 276 */       ps.close();
/* 277 */     } catch (SQLException se) {
/* 278 */       se.printStackTrace();
     }
   }

   private static void removeItemFromDB(int packageid, int charid) {
/* 283 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 284 */       PreparedStatement ps = con.prepareStatement("DELETE FROM dueypackages WHERE PackageId = ? AND RecieverId = ?");
/* 285 */       ps.setInt(1, packageid);
/* 286 */       ps.setInt(2, charid);
/* 287 */       ps.executeUpdate();
/* 288 */       ps.close();
/* 289 */       ItemLoader.送货道具.saveItems(con, null, packageid);
/* 290 */     } catch (SQLException se) {
/* 291 */       se.printStackTrace();
     }
   }

   private static MapleDueyActions getItemByPID(int packageid) {
     try {
/* 297 */       Map<Long, Pair<Item, MapleInventoryType>> iter = ItemLoader.送货道具.loadItems(false, packageid);
/* 298 */       if (iter != null && iter.size() > 0) {
/* 299 */         Iterator<Pair<Item, MapleInventoryType>> iterator = iter.values().iterator(); if (iterator.hasNext()) { Pair<Item, MapleInventoryType> i = iterator.next();
/* 300 */           return new MapleDueyActions(packageid, (Item)i.getLeft()); }

       }
/* 303 */     } catch (Exception se) {
/* 304 */       se.printStackTrace();
     }
/* 306 */     return new MapleDueyActions(packageid);
   }

   private static int getAccIdFromName(String name, boolean accountid) {
/* 310 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 311 */       PreparedStatement ps = con.prepareStatement("SELECT * FROM characters WHERE name = ?");
/* 312 */       ps.setString(1, name);
/* 313 */       ResultSet rs = ps.executeQuery();
/* 314 */       if (!rs.next()) {
/* 315 */         rs.close();
/* 316 */         ps.close();
/* 317 */         return -1;
       }
/* 319 */       int id_ = accountid ? rs.getInt("accountid") : rs.getInt("id");
/* 320 */       rs.close();
/* 321 */       ps.close();
/* 322 */       return id_;
/* 323 */     } catch (SQLException sQLException) {

/* 325 */       return -1;
     }
   }
 }


/* Location:              C:\PGX\Server.jar!\handling\channel\handler\DueyHandler.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */